package com.atom.common.activiti;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.task.Task;
import org.springframework.stereotype.Component;

import com.atom.common.dao.HistoricTaskDao;
import com.atom.common.entity.HistoricTaskDO;
import com.atom.common.remote.palace.RemoteHistoricTaskDO;
import com.atom.common.remote.palace.RemoteHistoricTaskDao;
@Component
public class HistoricTaskHandler {

  @Resource
  private HistoricTaskDao historicTaskDao;
  @Resource
  private RemoteHistoricTaskDao remoteHistoricTaskDao;

  /**
   * Description: 写入已办列表
   * @param task
   * @param variables
   * @param completeType
   */
  public void insert(Task task, Map<String, Object> variables, int completeType) {
    HistoricTaskDO taskDO =  HistoricTaskDO.getHistoricTaskDO(task, variables, completeType);
    historicTaskDao.save(taskDO);    
  }

  /**
   * Description: 写入平台已办列表
   * @param task
   * @param variables
   * @param completeType
   */
  public void insertRemote(Task task, Map<String, Object> variables, int completeType) {
    RemoteHistoricTaskDO taskDO = RemoteHistoricTaskDO.getRemoteHistoricTaskDO(task, variables, completeType);
    remoteHistoricTaskDao.save(taskDO);    
  }

	/**
	 * getDbListByUser 根据userID获取所有已办表中受理人为此userID的任务
	 * 
	 * @author wangli
	 * @param userid
	 * @return
	 */
	public List<HistoricTaskDO> getListByUser(String userid) {
		try {
			List<HistoricTaskDO> list = historicTaskDao.findByUserId(userid);
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	public List<HistoricTaskDO> getListByUserAndPage(String userid, int start, int count) {
		try {
			List<HistoricTaskDO> list = historicTaskDao.findByPropertyAndPageAndOrder("assignee", userid, start, count,"finishTime","desc");
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	
	public List<HistoricTaskDO> getListByUserAndTitle(String userid,
			String titlename) {
		try {
			List<HistoricTaskDO> list = historicTaskDao.findByPropertys(
					"assignee", "processInstanceName", userid, titlename);
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	public List<HistoricTaskDO> getListByUserAndTitleAndPage(String userid,
			String titlename, int start, int count) {
		try {
			List<HistoricTaskDO> list = historicTaskDao.findByPropertysAndPageAndOrder(
					"assignee", "processInstanceName", userid, titlename,start, count,"finishTime","desc");
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	
	public HistoricTaskDO getByTaskId(String taskid){
		List<HistoricTaskDO> list = historicTaskDao.findByProperty("taskId", taskid);
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	public HistoricTaskDO getLastByProcessIdAndTaskKey(String processId, String taskKey){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("processInstanceId", processId);
		map.put("taskDefinitionKey", taskKey);
		List<HistoricTaskDO> list = historicTaskDao.findByProperties(map);
		if(list!=null&&list.size()>0){
			return list.get(list.size()-1);
		}
		return null;
	}
  
  
}
